package cn.biplam.back.bean.mdmanage;

import java.io.Serializable;
import java.util.ArrayList;

import cn.biplam.common.bean.md.IndexTable;
import cn.biplam.common.client.DataFilter;
import cn.biplam.common.client.Operator;
import cn.biplam.common.client.OperatorResult;
import cn.biplam.common.opcode.SysManageCode.TreeIndexCode;
import cn.biplam.common.paramters.MdParam.Index_Node_Move_Type;
import cn.biplam.common.paramters.TreeParam.Index_Tree_Node_Type;
import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.sqlresult.SqlResult;
import cn.biplam.common.wrapper.tree.TreeIndexFields;

/**
 *  索引表操作类
 * @author XiaoRuxing Oct 14, 20093:54:29 PM
 * @version 1.0
 */
public class IndexTableImp extends IndexTable implements Serializable{

	private static final long serialVersionUID = -1;
	private String userId;
	private String indexTabName;
	private String indexId;
	private OperatorResult indexData;
	private TreeIndexFields treeFields;
	private static final String nodeType="1";

	/**
	 *  创建索引表访问接口,用于数据修改操作
	 * @param userId         用户信息
	 * @param indexTabName  索引表名称
	 * @throws ErrorMessage 
	 */
	public IndexTableImp(String userId, String indexTabName) throws ErrorMessage{
		this(userId,indexTabName,null);
	}

	/**
	 *  创建索引表访问接口,用于数据查询
	 * @param userId         用户信息
	 * @param indexTabName   索引表名称
	 * @param indexId        索引节点ID
	 * @throws ErrorMessage 
	 */
	public IndexTableImp(String userId, String indexTabName, String indexId) throws ErrorMessage{
		if(indexTabName==null){
			throw new ErrorMessage("Index Table Name is null at IndexTableImp!");
		}
		this.userId = userId;
		this.indexTabName = indexTabName;
		this.indexId = indexId;
		_init();
	}

	/**
	 *  更新索引表
	 *  @param nodeId   节点ID
	 *  @param nodeName 节点名称
	 * @throws ErrorMessage
	 */
	public boolean update(String nodeId,String nodeName) throws ErrorMessage{
		return Operator.get(TreeIndexCode.Update_Index_Node, userId, 
				getUpdateParams(nodeId,treeFields.getNodeName(),nodeName)).dmlSuccess();
	}

	/**
	 *  获取更新索引表的SQL语句
	 *  @param nodeId    索引Id
	 *  @param nodeName  索引名称
	 */
	public ArrayList<String> getUpdateSql(String nodeId, String nodeName)
	throws ErrorMessage {
		OperatorResult or = Operator.get(TreeIndexCode.Update_Index_Node, userId, 
				getUpdateParams(nodeId,treeFields.getNodeName(),nodeName),true);
		if(or!=null){
			return or.getSqls();
		}else{
			return null;
		}
	}

	/**
	 *  传入索引ID,插入记录到索引表
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insertNode(String nodeID, String parentId, String nodeName,
			String operatorType) throws ErrorMessage {
		return this.insertNode(nodeID, parentId, nodeName, null, operatorType);
	}
	
	/**
	 *  传入索引ID,获取插入记录到索引表的SQL
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     真实表节点ID
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insertNode(String nodeID, String parentId, String nodeName,
			String nodeCode, String operatorType) throws ErrorMessage {
		return this.insertNode(nodeID, parentId, nodeName, nodeCode, operatorType, null);
	}
	
	/**
	 *  传入索引ID,插入记录到索引表
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     真实表节点ID
	 * @param operatorType 节点操作类型
	 * @param nodeDesc     节点描述
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insertNode(String nodeID, String parentId, String nodeName,
			String nodeCode, String operatorType, String nodeDesc)
			throws ErrorMessage {
		DataFilter filter = getInsertParam(nodeID,parentId, nodeName, nodeCode, operatorType,nodeDesc);
		return Operator.get(TreeIndexCode.Insert_By_Index_Id, userId, filter).getLastRowId();
	}
	
	/**
	 *  传入索引ID,获取插入记录到索引表的SQL
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertNodeSql(String nodeID, String parentId,
			String nodeName, String operatorType) throws ErrorMessage {
		return this.getInsertNodeSql(nodeID, parentId, nodeName, null, operatorType);
	}

	/**
	 *  传入索引ID,插入记录到索引表
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     真实表节点ID
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertNodeSql(String nodeID, String parentId,
			String nodeName, String nodeCode, String operatorType)
	throws ErrorMessage {
		return this.getInsertNodeSql(nodeID, parentId, nodeName, nodeCode, operatorType,null);
	}
	
	/**
	 *  传入索引ID,插入记录到索引表
	 * @param nodeID      索引ID
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     真实表节点ID
	 * @param operatorType 节点操作类型
	 * @param nodeDesc     节点描述
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertNodeSql(String nodeID, String parentId,
			String nodeName, String nodeCode, 
			String operatorType,String nodeDesc)
	throws ErrorMessage {
		DataFilter filter = getInsertParam(nodeID,parentId, nodeName,
				nodeCode, operatorType, nodeDesc);
		return Operator.get(TreeIndexCode.Insert_By_Index_Id, userId, filter,true);
	}

	/**
	 *  插入记录到索引表
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insert(String parentId, String nodeName, String operatorType)
	throws ErrorMessage {
		return this.insert(parentId, nodeName, null, operatorType);
	}

	/**
	 *  插入记录到索引表
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     节点代码ID
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public String insert(String parentId, String nodeName, String nodeCode,
			String operatorType) throws ErrorMessage {
		return this.insert(parentId, nodeName, nodeCode, operatorType, null);
	}

	/**
	 *  插入记录到索引表
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     节点代码ID
	 * @param operatorType 节点操作类型
	 * @param nodeDesc     节点描述
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public String insert(String parentId,String nodeName,
			String nodeCode,String operatorType, String nodeDesc) throws ErrorMessage{
		DataFilter filter = getInsertParam(null,parentId, 
				nodeName, nodeCode, operatorType, nodeDesc);
		return Operator.get(TreeIndexCode.Insert_By_Index_Id, userId, filter).getLastRowId();
	}

	/**
	 *  获取插入记录到索引表的SQL语句
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public OperatorResult getInsertSql(String parentId, String nodeName,
			String operatorType) throws ErrorMessage {
		return this.getInsertSql(parentId, nodeName, null, operatorType);
	}

	/**
	 *  获取插入记录到索引表的SQL语句
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     节点代码ID
	 * @param operatorType 节点操作类型
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public OperatorResult getInsertSql(String parentId, String nodeName,
			String nodeCode, String operatorType) throws ErrorMessage {
		return this.getInsertSql(parentId, nodeName, nodeCode, operatorType, null);
	}

	/**
	 *  获取插入记录到索引表的SQL语句
	 * @param parentId     父节点ID
	 * @param nodeName     节点名称
	 * @param nodeCode     节点代码ID
	 * @param operatorType 节点操作类型
	 * @param nodeDesc     节点描述
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertSql(String parentId, String nodeName,
			String nodeCode, String operatorType, String nodeDesc)
	throws ErrorMessage {
		DataFilter filter = getInsertParam(null, parentId, nodeName, 
				nodeCode, operatorType, nodeDesc);
		return Operator.get(TreeIndexCode.Insert_By_Index_Id, userId, filter,true);
	}

	/**
	 *  插入根节点到索引表,默认父虚拟根节点为-1
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public String insertRoot(String nodeName,String nodeCode) throws ErrorMessage{
		return this.insertRoot(null, nodeName, nodeCode);
	}
	
	/**
	 *  插入根节点到索引表
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 */
	public String insertRoot(String parentId,String nodeName,String nodeCode) throws ErrorMessage{
		return this.insertRoot(parentId, nodeName, null, nodeCode);
	}

	/**
	 *  获取插入根节点到索引表,默认父虚拟根节点为-1的SQL
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public OperatorResult getInsertRootSql(String nodeName,String nodeCode) throws ErrorMessage{
		return getInsertRootSql(null, nodeName, nodeCode);
	}
	
	/**
	 *  获取插入根节点到索引表SQL
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	public OperatorResult getInsertRootSql(String parentID,String nodeName,String nodeCode) throws ErrorMessage {
		return getInsertRootSql(parentID, nodeName, null, nodeCode);
	}

	/**
	 *  传入节点ID,插入根节点到索引表,节点名称和代码可以为空
	 * @param nodeId      节点ID
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insertRootByNodeId(String nodeId, String parentId,
			String nodeName, String nodeCode) throws ErrorMessage {
		return insertRootByNodeId(nodeId, parentId, nodeName, nodeCode,null);
	}

	/**
	 *  传入节点ID,插入根节点到索引表,节点名称和代码可以为空
	 * @param nodeId      节点ID
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public String insertRootByNodeId(String nodeId, String parentId,
			String nodeName, String nodeCode, String nodeDesc)
	throws ErrorMessage {
		return insertRootByIndexId(nodeId, parentId, nodeName, nodeCode,nodeDesc);
	}

	/**
	 *  传入根节点ID,获取插入根节点到索引表的SQL,节点名和代码可以为空
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertRootByNodeIdSql(String nodeId,
			String parentId, String nodeName, String nodeCode)
	throws ErrorMessage {
		return insertRootByIndexIdSql(nodeId, parentId, nodeName, nodeCode, null);
	}
	
	/**
	 *  传入根节点ID,获取插入根节点到索引表的SQL,节点名和代码可以为空
	 * @param parentID     父虚拟节点ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点代码ID
	 * @param nodeDesc    节点描述
	 * @return 插入记录ID
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertRootByNodeIdSql(String nodeId,
			String parentId, String nodeName, String nodeCode, String nodeDesc)
	throws ErrorMessage {
		return insertRootByIndexIdSql(nodeId, parentId, nodeName, nodeCode,nodeDesc);
	}

	/**
	 * 插入虚拟根节点
	 * @param nodeId      节点ID
	 * @return OperatorResult
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult insertVirtualRoot(String nodeId) throws ErrorMessage {
		return this.insertVirtualRoot(nodeId,null);
	}
	
	/**
	 *  插入虚拟根节点
	 * @param nodeId      节点ID
	 * @param nodeCode    节点真实ID
	 * @return 插入记录ID和执行结果
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult insertVirtualRoot(String nodeId, String nodeCode)
	throws ErrorMessage {
		return this.insertVirtualRoot(nodeId, nodeCode, null, null);
	}
	
	/**
	 * 插入虚拟根节点
	 * @param nodeId      节点ID
	 * @param nodeCode    节点代码
	 * @param nodeName    节点名称
	 * @param nodeDesc    节点描述
	 * @return 插入结果
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult insertVirtualRoot(String nodeId, String nodeCode,
			String nodeName, String nodeDesc) throws ErrorMessage {
		return this._insertVirtualRoot(nodeId, nodeCode,nodeName,
				nodeDesc,false);
	}
	
	/**
	 * 获取插入虚拟根节点的SQL
	 * @param nodeId      节点ID
	 * @return OperatorResult
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertVirtualRootSql(String nodeId) throws ErrorMessage {
		return this.getInsertVirtualRootSql(nodeId, null);
	}

	/**
	 *  获取插入虚拟根节点的SQL
	 * @param nodeId      节点ID
	 * @param nodeCode    节点真实ID
	 * @return 插入记录ID和执行结果
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertVirtualRootSql(String nodeId, String nodeCode)
	throws ErrorMessage {
		return this.getInsertVirtualRootSql(nodeId, nodeCode,null,null);
	}

	/**
	 *  获取插入虚拟根节点的SQL
	 * @param nodeId      节点ID
	 * @param nodeCode    节点真实ID
	 * @param nodeName    节点名称
	 * @param nodeDesc    节点描述
	 * @return 插入记录ID和执行结果
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getInsertVirtualRootSql(String nodeId,
			String nodeCode, String nodeName, String nodeDesc)
	throws ErrorMessage {
		return this._insertVirtualRoot(nodeId, nodeCode,nodeName,
				nodeCode,true);
	}

	/**
	 *  删除索引记录
	 * @param nodeId 索引ID
	 * @throws ErrorMessage
	 */
	public OperatorResult delete(String nodeId) throws ErrorMessage{
		/** 如果有子节点则不能删除，抛出异常 */
		DataFilter filter = new DataFilter(treeFields.getNodeId(), nodeId);
		setTableName(filter);
		SqlResult childs=Operator.get(TreeIndexCode.Get_Numb_Of_Child, userId, filter).getSelectResult();
		int childsNumb=TOOLS.stringToInt(childs.get(0, 0));
		if(childsNumb>0){
			throw new ErrorMessage("Can't be delete for have child");
		}
		return this._delete(nodeId, false);
	}

	/**
	 *  获取删除索引记录的SQL语句
	 * @param nodeId 索引ID
	 * @throws ErrorMessage
	 * @return 如果索引不存在返回NULL
	 */
	@Override
	public OperatorResult getDeleteSql(String nodeId) throws ErrorMessage {
		return this._delete(nodeId, true);
	}

	/**
	 * 删除虚根节点
	 * @param nodeId 节点ID
	 * @return 删除是否成功
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult deleteVirtualNode(String nodeId) throws ErrorMessage {
		return this._deleteVirtualNode(nodeId, false);
	}

	/**
	 * 获取删除虚根节点的SQL
	 * @param nodeId 节点ID
	 * @return 删除虚拟根节点SQL
	 * @throws ErrorMessage
	 */
	@Override
	public OperatorResult getDeleteVirtualNodeSql(String nodeId) throws ErrorMessage {
		return this._deleteVirtualNode(nodeId, true);
	}

	/**
	 *  索引表移动
	 * @param srcId    被移动节点ID
	 * @param trgId    移动目标节点ID
	 * @param moveType 移动类型
	 * @throws ErrorMessage 
	 */
	public void move(String srcId, String trgId, Index_Node_Move_Type moveType) throws ErrorMessage{
		DataFilter treeFilter = new DataFilter();
		setTableName(treeFilter);
		treeFilter.put("moved_node_id", srcId);
		treeFilter.put("move_to_node_id", trgId);
		treeFilter.put("move_type", moveType);
		Operator.get(TreeIndexCode.Move_By_Index_Id, userId, treeFilter);
	}

	
	/**
	 * 向上查找两个节点之间的路径
	 * @param startNode 起始节点
	 * @param endNode   终止节点
	 * @return  终止节点为空时返回起始节点到根节点的路径<BR>
	 * 		   当起始节点或者终止节点不存在时:返回NULL<BR>
	 *         起始节点和终止节点间不存在路径时:返回空列表<BR>
	 *         终止节点开始到起始节点间起始节点的所有父亲(包含终止节点)<BR>
	 * @throws ErrorMessage 
	 */
	@Override
	public ArrayList<String> findPath(String startNode, String endNode) throws ErrorMessage {
		//如果起始节点是终止节点返回空List
		if(startNode.equals(endNode))return new ArrayList<String>();
		
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(), startNode);
		filter.put(treeFields.getNodeParent(), endNode);
		
		//当起始节点或者终止节点不存在时:返回NULL
		OperatorResult sNode = this.getNode(startNode);
		if(sNode.size()<1) 
			return null;
		
		//查询起始节点和终止节点间所有父亲
		ArrayList<String> list = Operator.get(TreeIndexCode.Select_Path, userId, filter).getListResult();
		
		//起始节点和终止节点间不存在路径时:返回空的List
		if(endNode!=null && list.indexOf(endNode)<0)
			return new ArrayList<String>();
		return list;
	}


	@Override
	public String getNodeCode() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getRealId());
		}else{
			return null;
		}
	}



	@Override
	public String getNodeDesc() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeDesc());
		}else{
			return null;
		}
	}



	@Override
	public String getNodeId() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeId());
		}else{
			return null;
		}
	}



	@Override
	public String getNodeName() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeName());
		}else{
			return null;
		}
	}



	@Override
	public String getNodeParentId() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeParent());
		}else{
			return null;
		}
	}


	@Override
	public String getNodeType() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeType());
		}else{
			return null;
		}
	}

	/**
	 * @return boolean 是否是叶子节点
	 */
	@Override
	public boolean getIsLeaf() throws ErrorMessage{
		//查询节点的儿子
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(), indexId);
		OperatorResult rs = Operator.get(TreeIndexCode.Get_Immediate_Children, userId, filter);
		
		return TOOLS.exist(rs)?false:true;//如果节点有儿子返回false,否则返回true
	}

	@Override
	public String getOperatorType() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getOperatorType());
		}else{
			return null;
		}
	}

	/**
	 * @return boolean 指定节点是否存在
	 */
	public boolean isExist(){
		return TOOLS.exist(this.indexData);
	}

	private void _init() throws ErrorMessage{
		treeFields=TreeIndexFields.getTreeFields(indexTabName);
		if(TOOLS.StringIsNull(this.indexId)){
			return;
		}
		indexData = getNode(indexId);
	}

	/**
	 *  根据节点ID获取节点
	 * @param indexId 节点ID
	 * @throws ErrorMessage
	 */
	private OperatorResult getNode(String indexId) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(), indexId);
		return Operator.get(TreeIndexCode.Select_Node, userId, filter);
	}
	
	/**
	 *  设置表名称
	 */
	private void setTableName(DataFilter filter) throws ErrorMessage{
		filter.put("table_name", indexTabName);
	}

	/**
	 *  设置索引表更新参数
	 */
	private DataFilter getUpdateParams(String nodeId,String key,String value) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(),nodeId);
		filter.put(key,value);
		return filter;
	}

	/**
	 *  设置添加记录参数
	 * @param nodeId        记录ID
	 * @param parentId      父亲ID
	 * @param nodeName      节点名称
	 * @param nodeCode      节点真实ID
	 * @param operatorType  节点类型
	 * @param nodeDesc      节点描述
	 * @return
	 * @throws ErrorMessage
	 */
	private DataFilter getInsertParam(String nodeId,String parentId, String nodeName,
			String nodeCode, String operatorType,String nodeDesc) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(),nodeId);
		filter.put(treeFields.getNodeParent(),parentId);
		filter.put(treeFields.getNodeName(),nodeName);
		filter.put(treeFields.getRealId(),nodeCode);
		filter.put(treeFields.getOperatorType(),operatorType);
		filter.put(treeFields.getNodeDesc(), nodeDesc);
		filter.put(treeFields.getNodeType(), nodeType);
		return filter;
	}

	/**
	 *  传入节点ID,插入根节点到索引表
	 */
	private String insertRootByIndexId(String nodeId,String parentId,
			String nodeName,String nodeCode,
			String nodeDesc) throws ErrorMessage{
		DataFilter filter = getInsertParam(nodeId,
				parentId, 
				nodeName, 
				nodeCode, 
				Index_Tree_Node_Type.ROOT_THEME.toString(), 
				nodeDesc);
		return Operator.get(TreeIndexCode.Insert_Root, userId, filter).getLastRowId();
	}

	/**
	 *  传入节点ID,插入根节点到索引表的SQL
	 */
	private OperatorResult insertRootByIndexIdSql(String nodeId,
			String parentId,
			String nodeName,
			String nodeCode,
			String nodeDesc) throws ErrorMessage{
		DataFilter filter = getInsertParam(nodeId , parentId, 
				nodeName, 
				nodeCode, 
				Index_Tree_Node_Type.ROOT_THEME.toString(), 
				nodeDesc);
		return Operator.get(TreeIndexCode.Insert_Root, userId, filter,true);
	}


	/**
	 *  插入根节点到索引表
	 */
	private String insertRoot(String parentId,
			String nodeName,
			String nodeDesc,
			String nodeCode) throws ErrorMessage{
		DataFilter filter = getInsertParam(null,parentId, nodeName, nodeCode, 
				Index_Tree_Node_Type.ROOT_THEME.toString(),
				nodeDesc);
		return Operator.get(TreeIndexCode.Insert_Root, userId, filter).getLastRowId();
	}

	/**
	 *  插入根节点到索引表的SQL
	 */
	private OperatorResult getInsertRootSql(String parentId,String nodeName,String nodeDesc,
			String nodeCode) throws ErrorMessage{
		DataFilter filter = getInsertParam(null,parentId, nodeName, nodeCode, 
				Index_Tree_Node_Type.ROOT_THEME.toString()
				,nodeDesc);
		return Operator.get(TreeIndexCode.Insert_Root, userId, filter,true);
	}
	
	/**
	 * 删除节点
	 * @param nodeId
	 * @param ifgetsql		是否返回SQL
	 * @return	OperatorResult
	 * @throws ErrorMessage
	 */
	private OperatorResult _delete(String nodeId,boolean ifgetsql) throws ErrorMessage {
		OperatorResult or = null;
		DataFilter filter = new DataFilter();
		setTableName(filter);
		IndexTableImp iti = new IndexTableImp(userId,indexTabName,nodeId);
		if(!iti.isExist()){//如果节点不存在返回
			or=new OperatorResult();
			or.setDmlResult(false);
			or.setOperatorInfo("Node is not exists");
		}else{
			filter.put(treeFields.getNodeId(), nodeId);
			if(iti.getOperatorType().equals(Index_Tree_Node_Type.ROOT_THEME.toString())){//如果是根节点执行根节点删除
				or = Operator.get(TreeIndexCode.Delete_Root_By_Index_Id, userId, filter,ifgetsql);
			}else{
				or = Operator.get(TreeIndexCode.Delete_By_Index_Id, userId, filter,ifgetsql);
			}
		}
		return or;
	}

	/**
	 *  插入虚根节点
	 * @param nodeId       节点ID
	 * @param nodeCode     节点真实代码
	 * @param nodeName     节点名称
	 * @param nodeDesc     节点描述
	 * @param isGetSql 是否返回SQL
	 * @return 插入是否成功
	 * @throws ErrorMessage
	 */
	private OperatorResult _insertVirtualRoot(String nodeId, String nodeCode,
			String nodeName,
			String nodeDesc,
			boolean isGetSql) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(), nodeId);
		filter.put(treeFields.getRealId(), nodeCode);
		filter.put(treeFields.getNodeName(), nodeName);
		filter.put(treeFields.getNodeDesc(), nodeDesc);
		return Operator.get(TreeIndexCode.Insert_Virtual_Root, userId, filter,isGetSql);
	}
	
	/**
	 * 删除虚根节点
	 * @param nodeId
	 * @param isGetSql		是否返回SQL
	 * @return
	 * @throws ErrorMessage
	 */
	private OperatorResult _deleteVirtualNode(String nodeId,boolean isGetSql) throws ErrorMessage{
		DataFilter filter = new DataFilter();
		setTableName(filter);
		filter.put(treeFields.getNodeId(), nodeId);
		return Operator.get(TreeIndexCode.Delete_Virtual_Root, userId, filter,isGetSql);
	}

	@Override
	public String getNodeSequence() throws ErrorMessage {
		if(TOOLS.exist(indexData)){
			return indexData.getValue(treeFields.getNodeSequence());
		}else{
			return null;
		}
	}

	@Override
	public ArrayList<String> getUpdateSqlCode(String nodeId, String nodeCode)
			throws ErrorMessage {
		OperatorResult or = Operator.get(TreeIndexCode.Update_Index_Node, userId, 
				getUpdateParams(nodeId,treeFields.getRealId(),nodeCode),true);
		if(or!=null){
			return or.getSqls();
		}else{
			return null;
		}
	}

	@Override
	public boolean updateCode(String nodeId, String nodeCode)
			throws ErrorMessage {
		return Operator.get(TreeIndexCode.Update_Index_Node, userId, 
				getUpdateParams(nodeId,treeFields.getRealId(),nodeCode)).dmlSuccess();
	}
}
